模型代码设计
以留言信息管理为例。
获取根据条件查询记录总数和分页数据。
query($sql, null); return $result[0]["total"]; } /**分页留言信息查询*/ public function list($pageIndex = 1, $pageSize = 10, $like = null) { $sql=null; if ($like != null) { $sql = "SELECT * FROM guestbook WHERE uname LIKE '%" . $like . "%' ORDER BY id DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize; }else{ $sql = "SELECT * FROM guestbook ORDER BY id DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize; } return $this->query($sql, null); }}
控制器代码设计
控制器接受从视图发起的数据请求。
从数据模型获取分页数据,以json格式返回视图。
display(); } /**分页留言数据*/ public function listgb() { $pageIndex = isset($_GET['pageIndex']) ? $_GET['pageIndex'] : 1; $pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 10; $like = isset($_GET['like']) ? $_GET['like'] : null; $gm = new GbmgrModel(); //获得符合条件的总记录数 $totalCount = $gm->count($like); //获得分页数据 $list = $gm->list($pageIndex, $pageSize, $like); //总页数 $totalPage = $totalCount % $pageSize == 0 ? $totalCount / $pageSize : intval($totalCount / $pageSize) + 1; //上一页 $previous = $pageIndex <= 1 ? 1 : $pageIndex - 1; //下一页 $next = $pageIndex >= $totalPage ? $totalPage : $pageIndex + 1; $result = ["totalCount" => $totalCount, "totalPage" => $totalPage, "pageIndex" => $pageIndex, "previous" => $previous, "next" => $next, "list" => $list]; echo json_encode($result, JSON_UNESCAPED_UNICODE); }}
视图脚本
测试效果